

#pragma once

#include "esp_lcd_touch.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Create a new NS2009 touch driver
 *
 * @note The I2C communication should be initialized before use this function.
 *
 * @param io LCD/Touch panel IO handle
 * @param config: Touch configuration
 * @param out_touch: Touch instance handle
 * @return
 *      - ESP_OK                    on success
 *      - ESP_ERR_NO_MEM            if there is no memory for allocating main structure
 */
esp_err_t esp_lcd_touch_new_i2c_ns2009(const esp_lcd_panel_io_handle_t io, const esp_lcd_touch_config_t *config, esp_lcd_touch_handle_t *out_touch);

/**
 * @brief I2C address of the NS2009 controller
 *
 */
#define ESP_LCD_TOUCH_IO_I2C_NS2009_ADDRESS (0x48)
// #define ESP_LCD_TOUCH_IO_I2C_NS2009_ADDRESS (0x5B)

// REG defination
#define INPUT_PORT0  0x00    // R
#define INPUT_PORT1  0x01    // R
#define OUTPUT_PORT0 0x02    // RW
#define OUTPUT_PORT1 0x03    // RW
#define CONFIG_PORT0 0x04    // RW
#define CONFIG_PORT1 0x05    // RW
#define INT_PORT0    0x06    // RW
#define INT_PORT1    0x07    // RW
#define REG_ID       0x10    // R
#define REG_CTL      0x11    // WR, Global Ctrl
#define MODE_PORT0   0x12    // WR
#define MODE_PORT1   0x13    // WR
#define DIM0         0x20    // W, P1_0 LED current control
#define DIM1         0x21    // W, P1_1 LED current control
#define DIM2         0x22    // W, P1_2 LED current control
#define DIM3         0x23    // W, P1_3 LED current control
#define DIM4         0x24    // W, P0_0 LED current control
#define DIM5         0x25    // W, P0_1 LED current control
#define DIM6         0x26    // W, P0_2 LED current control
#define DIM7         0x27    // W, P0_3 LED current control
#define DIM8         0x28    // W, P0_4 LED current control
#define DIM9         0x29    // W, P0_5 LED current control
#define DIM10        0x2A    // W, P0_6 LED current control
#define DIM11        0x2B    // W, P0_7 LED current control
#define DIM12        0x2C    // W, P1_4 LED current control
#define DIM13        0x2D    // W, P1_5 LED current control
#define DIM14        0x2E    // W, P1_6 LED current control
#define DIM15        0x2F    // W, P1_7 LED current control
#define SW_RSTN      0x7F    // W, Soft reset

/**
 * @brief Touch IO configuration structure
 *
 */
#define ESP_LCD_TOUCH_IO_I2C_NS2009_CONFIG()           \
    {                                       \
        .dev_addr = ESP_LCD_TOUCH_IO_I2C_NS2009_ADDRESS, \
        .control_phase_bytes = 1,           \
        .dc_bit_offset = 0,                 \
        .lcd_cmd_bits = 8,                 \
        .flags =                            \
        {                                   \
            .disable_control_phase = 1,     \
        }                                   \
    }

#ifdef __cplusplus
}
#endif
